package com.movie.service.impl;

import com.movie.dto.MovieDTO;
import com.movie.mapper.MovieMapper;
import com.movie.service.MovieService;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;

import java.util.List;

@Service
public class MovieServiceImpl implements MovieService {

    @Resource
    private MovieMapper movieMapper;

    @Override
    public List<MovieDTO> getAllMovies(Integer userId) {
        return movieMapper.selectAllMoviesWithUserInfo(userId);
    }

    @Override
    public MovieDTO getMovieDetail(Integer id, Integer userId) {
        return movieMapper.selectMovieDetail(id, userId);
    }

    @Override
    public List<MovieDTO> searchMovies(String keyword) {
        return movieMapper.selectMoviesByKeyword(keyword);
    }

    @Override
    public List<MovieDTO> getMoviesByCategory(String category, Integer userId) {
        return movieMapper.selectMoviesByCategory(category, userId);
    }

    @Override
    public List<MovieDTO> getTopRatedMovies(Integer limit) {
        return movieMapper.selectTopRatedMovies(limit);
    }

    @Override
    public List<MovieDTO> recommendMovies(Integer userId) {
        // 先查用户最喜欢的类别
        String category = movieMapper.findFavoriteCategoryByUser(userId);
        if (category == null) {
            // 没有评分记录，直接推荐热门电影
            return movieMapper.selectTopRatedMovies(10);
        }
        // 推荐该类别下没看过的电影
        return movieMapper.findMoviesByCategoryExcludeRated(category, userId);
    }
}
